<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            @click="openDrawer('addmj')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list" stripe>
        <el-table-column prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column prop="creator" label="作者" width="200">
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="200">
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('lookmj', row.id)"></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('editmj', row.id)"
              ></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        background
        layout=" prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- drawer -->
    <el-drawer
      size="50%"
      :visible.sync="drawer"
      direction="rtl"
      :title="drawerTitle"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="handleBlur"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  getArticleList,
  submitmianjing,
  delmianjing,
  dangemianjing,
  xiugaimianjing
} from '@/api/article.js'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  name: 'article-page',
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ],
      current: 1,
      pageSize: 10,
      total: 0,
      list: [],
      drawer: false,
      drawerType: 'addmj',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.initData()
  },
  computed: {
    drawerTitle() {
      let title = '默认大标题'
      if (this.drawerType === 'addmj') title = '添加面经'
      if (this.drawerType === 'lookmj') title = '查看面经'
      if (this.drawerType === 'editmj') title = '修改面经'
      return title
    }
  },
  methods: {
    async initData() {
      const { data } = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = data.rows
      this.total = data.total
      console.log(data)
    },
    async del(id) {
      await delmianjing(id)
      this.$message.success('删除成功')
      this.initData()
    },
    handleCurrentChange(value) {
      this.current = value
      this.initData()
    },
    async openDrawer(value, id) {
      this.drawer = true
      this.drawerType = value
      if (value !== 'addmj') {
        const res = await dangemianjing(id)
        this.form = {
          ...res.data
        }
      }
      console.log(value)
    },
    handleClose() {
      this.drawer = false
      this.$refs.form.resetFields()
      console.log('关闭')
    },
    handleBlur() {
      this.$refs.form.validateField('content')
    },
    async submit() {
      try {
        if (this.drawerType === 'addmj') {
          // 效验表单
          await this.$refs.form.validate()
          // 发请求
          await submitmianjing(this.form)
          // 添加提示
          this.$message.success('添加成功')
          // 重新渲染
          this.current = 1
          this.initData()
          // 关闭弹窗
          this.handleClose()
        }
        if (this.drawerType === 'editmj') {
          const { id, stem, content } = this.form
          await xiugaimianjing({ id, stem, content })
        }
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
